from typing import List


def threeSumClosest(nums: List[int], target: int) -> int:
    res = [2**31-1, 2**31-1]
    nums.sort()
    n = len(nums)
    for i in range(n):
        left = i + 1
        right = n - 1
        while left < right:
            while right - 1 > left and nums[i] + nums[left] + nums[right-1] >= target:
                right -= 1
            s = nums[i] + nums[left] + nums[right]
            res = [abs(s - target), s] if res[0] > abs(s - target) else res
            if right - 1 > left:
                s = nums[i] + nums[left] + nums[right - 1]
                res = [target - s, s] if res[0] > target - s else res
            left += 1
    return res[1]


print(threeSumClosest([-1,0,1,1,55], 3))
